package com.newcoder.community.controller;

import com.newcoder.community.service.AlphaService;
import com.newcoder.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author edison
 * @version 2023
 * @date 2024/1/14 16:14
 */

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello() {
        return "Hello Spring Boot.";
    }

    @RequestMapping("/data") //浏览器访问 http://localhost:8080/community/alpha/data
    @ResponseBody
    public String getData() {
        return alphaService.find();
    }

    // cookie示例
    @RequestMapping(path = "/cookie/set", method = RequestMethod.GET) //浏览器访问 http://localhost:8080/community/alpha/cookie/set
    @ResponseBody
    public String setCookie(HttpServletResponse response) {
        // 创建cookie
        Cookie cookie = new Cookie("code", CommunityUtil.generateUUID());
        // 设置cookie生效范围
        cookie.setPath("/community/alpha");
        // 设置cookie生存时间
        cookie.setMaxAge(60 * 10);
        // 发送cookie
        response.addCookie(cookie);

        return "set cookie";
    }

    @RequestMapping(path = "/cookie/get", method = RequestMethod.GET) //浏览器访问
    @ResponseBody
    public String getCookie(@CookieValue("code") String code) {
        // 获取cookie
        System.out.println(code);

        return "get cookie";
    }

    // session示例
    @RequestMapping(path = "/session/set", method = RequestMethod.GET) //浏览器访问 http://localhost:8080/community/alpha/session/set
    @ResponseBody
    public String setSession(HttpSession session) {
        // 获取session
        session.setAttribute("id", 1);
        session.setAttribute("name", "test");

        return "set session";
    }

    @RequestMapping(path = "/session/get", method = RequestMethod.GET) //浏览器访问 http://lo calhost:8080/community/alpha/session/get
    @ResponseBody
    public String getSession(HttpSession session) {
        // 获取session
        System.out.println(session.getAttribute("id"));
        System.out.println(session.getAttribute("name"));

        return "get session";
    }

    // ajax示例
    @RequestMapping(path = "/ajax", method = RequestMethod.POST) //浏览器访问 http://localhost:8080/community/alpha/ajax
    @ResponseBody
    public String testAjax(String name, int age) {
        System.out.println(name);
        System.out.println(age);

        return CommunityUtil.getJSONString(0, "操作成功");
    }



}
